//
//  WorkTimeVC.m
//  QoilPublic
//
//  Created by SunChao on 2018/4/15.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "WorkTimeVC.h"
#import "SINavigationMenuView.h"
#import "WorkTimeCell.h"
@interface WorkTimeVC ()<SINavigationMenuDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *xDataSource;
@property (nonatomic,strong)NSMutableArray *yDataSource;
@property (nonatomic,strong)SINavigationMenuView *menu;
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation WorkTimeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(monthSelectedNotification:) name:@"MonthSelectedNotification" object:nil];
    [self requestData];
    self.view.backgroundColor = [UIColor colorWithRed:55.0/255.0f green:155.0/255.0f blue:233.0/255.0f alpha:1.0f];
    self.tbView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.tbView registerNib:[UINib nibWithNibName:@"WorkTimeCell" bundle:nil] forCellReuseIdentifier:@"WorkTimeCell"];
    //获取当前时间,日期
    [self.tbView setSeparatorInset:UIEdgeInsetsZero];
    
    [self.tbView setLayoutMargins:UIEdgeInsetsZero];
}
- (void)monthSelectedNotification:(NSNotification *)noti{
    
    [self requestDataWithMonth:noti.object];
    
}
- (UITableView *)tbView{
    
    if ( !_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tbView.delegate = self;
        _tbView.dataSource = self;
    }
    return _tbView;
}

#pragma mark -- 点击下拉框代理
- (void)didSelectItemAtIndex:(NSUInteger)index
{
    NSString *selmenu = [_menu.items objectAtIndex:index];
    _menu.menuButton.title.text = selmenu;
}
- (void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex pointIndex:(NSInteger)pointIndex{
    NSLog(@"Click Key on line %f, %f line index is %d and point index is %d",point.x, point.y,(int)lineIndex, (int)pointIndex);
    
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake(point.x, point.y, 100, 10);
    
    [self.lineChart addSubview:label];
    
}

- (void)userClickedOnLinePoint:(CGPoint)point lineIndex:(NSInteger)lineIndex{
    
    NSLog(@"Click on line %f, %f, line index is %d",point.x, point.y, (int)lineIndex);
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
     WorkTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorkTimeCell"];
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.section == 0) {

        return cell;
        
    }
    
    [cell setCellWithData:[self.dataSource objectAtIndex:indexPath.row]];
    
    return cell;
    
    
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [cell setSeparatorInset:UIEdgeInsetsZero];
    
    [cell setLayoutMargins:UIEdgeInsetsZero];
    
}


- (void)requestData{
    NSDate *currentDate = [NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"YYYY-MM"];
    NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
    NSLog(@"currentDateString:%@",currentDateString);
    NSString *url = [QParamService urlWithIOTAction:@"report/monthWorktime"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:[QParamService getToken] forKey:@"token"];
    [param setObject:currentDateString forKey:@"datetime"];
    
    [[QHttpManager sharedManager] POST:url parameterMD5jm:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if (error) {
            
            NSLog(@"error:%@",basModel.errMsg);
            
            return ;
        }
        
        if([basModel.result isEqualToString:@"success"]){
            
            NSLog(@"dic:%@",basModel.items);
            self.xDataSource = [basModel.items objectForKey:@"x"];
            self.yDataSource = [basModel.items objectForKey:@"y"];
            self.dataSource = [basModel.items objectForKey:@"reqportDeviceData"];
            NSArray *yArr = @[@"0",@"5",@"10",@"15",@"20",@"25",@"30"];
            self.lineChart = [PNLineChart linecharWithXLabels:self.xDataSource YLabels:yArr yFixedValueMax:24  YFixedValueMin:0 withFirstLineDataSourse:self.yDataSource withSecondLineDataSourse:nil withthThirdLineDataSourse:nil];
            [self.view addSubview:self.lineChart];
            self.lineChart.delegate = self;
            [self.view addSubview:self.tbView];
            self.tbView.frame = CGRectMake(0, 220, kScreenWidth, kScreenHeight-220);
            self.tbView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        }
    }];
}

- (void)requestDataWithMonth:(NSString *)month{
    
    NSString *url = [QParamService urlWithIOTAction:@"report/monthWorktime"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:[QParamService getToken] forKey:@"token"];
    [param setObject:month forKey:@"datetime"];
    
    [[QHttpManager sharedManager] POST:url parameterMD5jm:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if (error) {
            
            NSLog(@"error:%@",basModel.errMsg);
            
            return ;
        }
        
        if([basModel.result isEqualToString:@"success"]){
            
            NSLog(@"dic:%@",basModel.items);
            self.xDataSource = [basModel.items objectForKey:@"x"];
            self.yDataSource = [basModel.items objectForKey:@"y"];
            self.dataSource = [basModel.items objectForKey:@"reqportDeviceData"];
            NSArray *yArr = @[@"0",@"5",@"10",@"15",@"20",@"25",@"30"];
            self.lineChart = [PNLineChart linecharWithXLabels:self.xDataSource YLabels:yArr yFixedValueMax:24  YFixedValueMin:0 withFirstLineDataSourse:self.yDataSource withSecondLineDataSourse:nil withthThirdLineDataSourse:nil];
            [self.view addSubview:self.lineChart];
            self.lineChart.delegate = self;
            [self.view addSubview:self.tbView];
            self.tbView.frame = CGRectMake(0, 220, kScreenWidth, kScreenHeight-220);
            self.tbView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        }
    }];
}

ARRAY_INIT(xDataSource);
ARRAY_INIT(yDataSource);
ARRAY_INIT(dataSource);
@end
